package installer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;

import org.apache.log4j.Logger;

import sleuthkit.util.StaticHelper;

public class Installer {
	
	private static String fileSeparator = System.getProperty("file.separator");
	private static boolean createExampleData;
	private static Logger log = Logger.getLogger(Installer.class);

	public Installer() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StaticHelper.configLogger();
		
		Installer instance = new Installer();
		log.debug("Installer instance = new Installer();");
		log.debug("args = " + StaticHelper.stringsToString(args));
		if (args.length > 1){
			createExampleData=true;
		}
		log.debug("instance.createConfigFile(" + fileSeparator + "usr/local/bin,/Users/mattias/Documents/cases);");
		instance.createConfigFile(fileSeparator + "usr/local/bin","/Users/mattias/Documents/cases");
	}

	private void createConfigFile(String sluethkit, String caseFolder) {
		// TODO Auto-generated method stub
		Properties configuration = new Properties();
		configuration.setProperty("mmls",sluethkit + fileSeparator + "mmls");
		configuration.setProperty("fls", sluethkit + fileSeparator + "fls");
		configuration.setProperty("blkcalc", sluethkit + fileSeparator + "blkcalc");
		configuration.setProperty("blkcat", sluethkit + fileSeparator + "blkcat");
		configuration.setProperty("blkls", sluethkit + fileSeparator + "blkls");
		configuration.setProperty("blkstat", sluethkit + fileSeparator + "blkstat");
		configuration.setProperty("fcat", sluethkit + fileSeparator + "fcat");
		configuration.setProperty("ffind", sluethkit + fileSeparator + "ffind");
		configuration.setProperty("fls", sluethkit + fileSeparator + "fls");
		configuration.setProperty("fsstat", sluethkit + fileSeparator + "fsstat");
		configuration.setProperty("hfind", sluethkit + fileSeparator + "hfind");
		configuration.setProperty("icat", sluethkit + fileSeparator + "icat");
		configuration.setProperty("ifind", sluethkit + fileSeparator + "ifind");
		configuration.setProperty("ils", sluethkit + fileSeparator + "ils");
		configuration.setProperty("img_cat", sluethkit + fileSeparator + "img_cat");
		configuration.setProperty("img_stat", sluethkit + fileSeparator + "img_stat");
		configuration.setProperty("istat", sluethkit + fileSeparator + "istat");
		configuration.setProperty("jcat", sluethkit + fileSeparator + "jcat");
		configuration.setProperty("jls", sluethkit + fileSeparator + "jls");
		configuration.setProperty("mactime", sluethkit + fileSeparator + "mactime");
		configuration.setProperty("mmcat", sluethkit + fileSeparator + "mmcat");
		configuration.setProperty("mmls", sluethkit + fileSeparator + "mmls");
		configuration.setProperty("mmstat", sluethkit + fileSeparator + "mmstat");
		configuration.setProperty("tsk_comparedir", sluethkit + fileSeparator + "tsk_comparedir");
		configuration.setProperty("tsk_gettimes", sluethkit + fileSeparator + "tsk_gettimes");
		configuration.setProperty("tsk_loaddb", sluethkit + fileSeparator + "tsk_loaddb");
		configuration.setProperty("tsk_recover", sluethkit + fileSeparator + "tsk_recover");
		configuration.setProperty("case folder", caseFolder);
		
		log.debug("configuration.setProperty(\"mmls\"," +sluethkit + fileSeparator + "mmls);\n" +
				"configuration.setProperty(\"fls\"," + sluethkit + fileSeparator + "fls);\n" +
				"configuration.setProperty(\"blkcalc\","+ sluethkit + fileSeparator + "blkcalc);\n"+
				"configuration.setProperty(\"blkcat\","+ sluethkit + fileSeparator + "blkcat);\n"+
				"configuration.setProperty(\"blkls\","+ sluethkit + fileSeparator + "blkls);\n" +
				"configuration.setProperty(\"blkstat\","+ sluethkit + fileSeparator + "blkstat);\n" +
				"configuration.setProperty(\"fcat\","+ sluethkit + fileSeparator + "fcat);\n"+
				"configuration.setProperty(\"ffind\","+ sluethkit + fileSeparator + "ffind);\n"+
				"configuration.setProperty(\"fls\","+ sluethkit + fileSeparator + "fls);\n"+
				"configuration.setProperty(\"fsstat\","+ sluethkit + fileSeparator + "fsstat);\n"+
				"configuration.setProperty(\"hfind\","+ sluethkit + fileSeparator + "hfind);\n"+
				"configuration.setProperty(\"icat\","+ sluethkit + fileSeparator + "icat);\n"+
				"configuration.setProperty(\"ifind\","+ sluethkit + fileSeparator + "ifind);\n"+
				"configuration.setProperty(\"ils\","+ sluethkit + fileSeparator + "ils);\n"+
		"configuration.setProperty(\"img_cat\","+ sluethkit + fileSeparator + "img_cat);\n"+
		"configuration.setProperty(\"img_stat\","+ sluethkit + fileSeparator + "img_stat);\n"+
		"configuration.setProperty(\"istat\","+ sluethkit + fileSeparator + "istat);\n"+
		"configuration.setProperty(\"jcat\","+ sluethkit + fileSeparator + "jcat);\n"+
		"configuration.setProperty(\"jls\","+ sluethkit + fileSeparator + "jls);\n"+
		"configuration.setProperty(\"mactime\","+ sluethkit + fileSeparator + "mactime);\n"+
		"configuration.setProperty(\"mmcat\","+ sluethkit + fileSeparator + "mmcat);\n"+
		"configuration.setProperty(\"mmls\","+ sluethkit + fileSeparator + "mmls);\n"+
		"configuration.setProperty(\"mmstat\","+ sluethkit + fileSeparator + "mmstat);\n"+
		"configuration.setProperty(\"tsk_comparedir\","+ sluethkit + fileSeparator + "tsk_comparedir);\n"+
		"configuration.setProperty(\"tsk_gettimes\","+ sluethkit + fileSeparator + "tsk_gettimes);\n"+
		"configuration.setProperty(\"tsk_loaddb\","+ sluethkit + fileSeparator + "tsk_loaddb);\n"+
		"configuration.setProperty(\"tsk_recover\","+ sluethkit + fileSeparator + "tsk_recover);\n"+
		"configuration.setProperty(\"case folder\","+ caseFolder+");");
		
		log.debug("createExampleData = " + createExampleData);
		if (createExampleData){
			Random rand = new Random();
			int antal = rand.nextInt(12) + 1;
			log.debug("antal = " + antal);
			String caseNames = new String();
			//caseNames = "K-"+ rand.nextInt(10000) + "-" + rand.nextInt(10000);
			log.debug("caseNames = " + caseNames);
			for (int i = 0; i < antal; i++) {
				if (!caseNames.equalsIgnoreCase(""))
					caseNames = caseNames + ";";
				caseNames = caseNames + "K-"+ rand.nextInt(10000) + "-" + rand.nextInt(10000);
				log.debug(i +" caseNames = \"" + caseNames+"\"");
			}
			configuration.setProperty("caseName", caseNames);
			log.debug("configuration.setProperty(\"caseName\",\""+ caseNames+"\");");
			antal = rand.nextInt(12) + 1;
			log.debug("antal = " + antal);
			String investigator = new String();
			//investigator = "Investigator" + rand.nextInt(1500);
			log.debug("investigator = " + investigator);
			for (int i = 0; i < antal; i++) {
				if (!investigator.equalsIgnoreCase(""))
					investigator = investigator + ";";
				investigator = investigator + "Investigator" + rand.nextInt(1500);
				log.debug(i + " investigator = " + investigator);
			}
			configuration.setProperty("investigator", investigator);
			log.debug("configuration.setProperty(\"investigator\",\""+ investigator + "\");");
		}
		
		try {
			String filename = System.getProperty("user.home")+
					System.getProperty("file.separator")+".sleuthkit-gui"+fileSeparator + "sleuthkit-gui.config";
			log.debug("filename = " + filename);
			File file = new File (filename);
			file.getParentFile().mkdirs();
			FileOutputStream fos = new FileOutputStream(file);
			configuration.storeToXML(fos, "Config fil for sleuthkit-gui");
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			log.fatal("Tried to write configuration to file ", e);
			System.exit(4);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.fatal("Tried to write configuration to file ", e);
			System.exit(5);
		}
	}

}
